//
//  PlayerView.m
//  封装播放器
//
//  Created by douya on 15/1/16.
//  Copyright (c) 2015年 douya. All rights reserved.
//

#import "PlayerView.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation PlayerView
@synthesize delegate,volume;

//由于UIView默认的layer是CALayer，而AVPlayer只能添加在AVPlayerLayer上才能显示出来，因此这里需要改变一下默认的layer，将PlayerView的默认layer改为AVPlayerLayer
+(Class)layerClass
{
    return [AVPlayerLayer class];
}

-(AVPlayer *)player
{
    return [(AVPlayerLayer *)[self layer] player];
}

-(void)setPlayer:(AVPlayer *)player
{
    [(AVPlayerLayer *)[self layer] setPlayer:player];
}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    UITouch *touch=[touches anyObject];
    CGPoint touchPoint=[touch locationInView:self];
//    NSLog(@"began %f==%f",touchPoint.x,touchPoint.y);
    x=touchPoint.x;
    y=touchPoint.y;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesEnded:touches withEvent:event];
    UITouch *touch=[touches anyObject];
    CGPoint touchPoint=[touch locationInView:self];
//    NSLog(@"end %f==%f",touchPoint.x,touchPoint.y);
    
    if ((touchPoint.x-x)>=10&&(touchPoint.y-y)<=20&&(touchPoint.y-y)>=-20) {
//        NSLog(@"滑动快进");
        if ([delegate respondsToSelector:@selector(TouchSpeed)])
        {
            [delegate TouchSpeed];
        }
    }
    if ((x-touchPoint.x)>=10&&(touchPoint.y-y)<=20&&(touchPoint.y-y)>=-20) {
//        NSLog(@"滑动后退");
        if ([delegate respondsToSelector:@selector(TouchRetreat)])
        {
            [delegate TouchRetreat];
        }
    }
//    MPMusicPlayerController *mpc=[MPMusicPlayerController applicationMusicPlayer];
    if ((touchPoint.y-y)>=10&&(touchPoint.x-x)<=50&&(touchPoint.x-x)>=-50)
    {
//        NSLog(@"音量减少");
        if ((self.player.volume-0.1)<=0)
        {
            self.player.volume=0;
        }
        else
        {
            self.player.volume=self.player.volume-0.05;
        }
    }
    
    if ((y-touchPoint.y)>=50&&(touchPoint.x-x)<=50&&(touchPoint.x-x)>=-50)
    {
//        NSLog(@"音量增加");
        if ((self.player.volume+0.1)>=1)
        {
            self.player.volume=1;
        }
        else
        {
            self.player.volume=self.player.volume+0.05;
        }
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
